package com.yesep.learn.netty.tcp.protocol.session;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.yesep.learn.netty.tcp.protocol.common.Constants;

import cn.hutool.core.collection.CollectionUtil;

/**
 * @Description: 指令参数定义
 */
public class CPData {
	/**
	 * CP指令区参数
	 */
	private Map<String, String> param = new HashMap<>();
	/**
	   *  监测指标的监测值、数据标记等
	 */
	private Map<String, Map<String, String>> data = new HashMap<>();

	public void addParam(String key, String value) {
		param.put(key, value);
	}

	public void clearParam() {
		param.clear();
	}

	public Map<String, String> getParam() {
		return param;
	}

	public String getParam(String key) {
		return param.get(key);
	}

	public void addData(String polId, String type, String value) {
		Map<String, String> d = data.get(polId);
		if (null == d) {
			data.put(polId, d = new HashMap<>());
		}
		data.get(polId).put(type, value);
	}

	public void clearData() {
		data.clear();
	}

	public Map<String, Map<String, String>> getData() {
		return data;
	}

	public Map<String, String> getData(String polId) {
		return data.get(polId);
	}

	public String getData(String polId, String type) {
		Map<String, String> temp = data.get(polId);
		if (null != temp) {
			return temp.get(type);
		} else {
			return null;
		}
	}

	@Override
	public String toString() {
		List<String> list = new ArrayList<>();
		param.forEach((k, v) -> list.add(k + Constants.DATA_EQ + v));
		for (Map.Entry<String, Map<String, String>> entry : data.entrySet()) {
			String polId = entry.getKey();
			Map<String, String> d = entry.getValue();
			List<String> temp = new ArrayList<>();
			d.forEach((k, v) -> temp.add(polId + Constants.DATA_SPLIT_INTERVAL + k + Constants.DATA_EQ + v));
			list.add(CollectionUtil.join(temp, Constants.DATA_SPLIT_SAME));
		}
		String s = CollectionUtil.join(list, Constants.DATA_SPLIT);
		return s;
	}
}
